package com.matrix.im.domain.model;

import com.matrix.common.exception.BusinessException;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.Serial;
import java.io.Serializable;
import java.util.Date;

/**
 * @author 有点甜
 * @since 2025/8/2
 */
@Data
@Document(collection = "system_messages")
public class SystemMessage implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    @Id
    private String id;

    @Indexed
    private Long userId;

    private String title;
    private String content;
    private String type;
    private String relatedId;

    @Indexed
    private Boolean isRead;

    @Indexed
    private Date timestamp;

    private SystemMessage() {}

    public static SystemMessage create(Long userId, String title, String content, String type, String relatedId) {
        SystemMessage message = new SystemMessage();
        message.userId = userId;
        message.title = title;
        message.content = content;
        message.type = type;
        message.relatedId = relatedId;
        message.isRead = false;
        message.timestamp = new Date();
        return message;
    }

    public void markAsRead(Long operatorId) {
        if (!this.userId.equals(operatorId)) {
            throw new BusinessException("无权操作此消息");
        }
        this.isRead = true;
    }
}
